import { fileURLToPath, URL } from 'node:url'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'
import process from 'node:process'

import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// 获取命令行参数中的模式
const mode = process.env.MODE || 'development';

// 加载 .env 文件中的环境变量
const env = loadEnv(mode, process.cwd());


// https://vite.dev/config/
export default defineConfig({
  server: {
    host: "127.0.0.1",
    port: 18888,
    proxy: {
      '/user': {
        target: env.VITE_TARGET_URL,
        changeOrigin: true,
        rewrite: (path) => path
      },
      '/live/api/me/ws': {
        target: env.VITE_TARGET_URL,
        changeOrigin: true,
        ws: true
      },
      '/live': {
        target: env.VITE_TARGET_URL,
        changeOrigin: true
      },
      '/video': {
        target: env.VITE_TARGET_URL,
        changeOrigin: true,
      },
      '/product': {
        target: env.VITE_TARGET_URL,
        changeOrigin: true,
      },
      '/order': {
        target: env.VITE_TARGET_URL,
        changeOrigin: true,
      },
      '/ai': {
        target: env.VITE_TARGET_URL,
        changeOrigin: true,
      },
      '/chat/api/me/ws': {
        target: env.VITE_TARGET_URL,
        changeOrigin: true,
        ws: true
      },
      '/chat': {
        target: env.VITE_TARGET_URL,
        changeOrigin: true,
      },
      '/ai/api/public/ws/voice-chat': {
        target: env.VITE_TARGET_URL,
        changeOrigin: true,
        ws: true
      },
      // '/video/ws/hand': {
      //   target: "http://127.0.0.1:10102",
      //   changeOrigin: true,
      //   ws: true
      // },
      // '/video/ws/camera': {
      //   target: "http://127.0.0.1:10101",
      //   changeOrigin: true,
      //   ws: true
      // },
    },
    hmr: {
      host: '127.0.0.1',
      port: 18888,
      protocol: 'ws'
    }
  },
  plugins: [
    vue(),
    vueJsx(),
    vueDevTools(),
    AutoImport({
      // 自动导入 Vue、Vue-Router 相关函数
      // imports: ['vue',  'vue-router'],
      // 自动导入 Element Plus 相关函数
      resolvers: [ElementPlusResolver({ importStyle: 'sass' })]
    }),
    Components({
      // 自动导入 Element Plus 组件
      resolvers: [ElementPlusResolver({ importStyle: 'sass' })]
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  }
})
